const AdminModel = require('../db/models/admin');

class AdminService {
  async addAdminData (adminInfo) {
    const { username } = adminInfo;
    
    const result = await AdminModel.findOne({
      where: {username}
    });

    if(result) {
      return await AdminModel.update(adminInfo, {
        where: {username}
      })
    }else {
      return await AdminModel.create(adminInfo);
    }
  }

  /* 登录数据判断*/
  async loginCheckInfo (userInfo) {
    const {username, password} = userInfo;

    //用户数据
    const usernameExit = await AdminModel.findOne({
      where: {username}
    });


    if(!usernameExit) {
      return 10003; //用户名不存在
    }
    
    const dbPassword = usernameExit.get('password');
    if(password != dbPassword) {
      return 10004; //密码错误
    }

    const uid = usernameExit.get('id');

    //成功
    return {
      uid,
      username
    }
  }
}

module.exports = new AdminService();